package com.kc.gb.device.service.impl;

import com.alibaba.fastjson.JSON;
import com.kc.gb.device.common.dto.I18NException;
import com.kc.gb.device.common.enums.ErrorCode;
import com.kc.gb.device.plugins.ptl.dto.PtlLightState;
import com.kc.gb.device.plugins.ptl.service.PtlHardwareService;
import com.kc.gb.device.service.ContainerPtlAminService;
import com.kc.gb.device.service.StripAdminService;
import com.kc.gb.device.service.support.dto.StationContainerSlotLayout;
import com.kc.gb.feign.ptl.support.dtos.AbstractPtlCommand;
import com.kc.gb.feign.ptl.support.dtos.LedCommand;
import com.kc.gb.feign.ptl.support.dtos.LightCommand;
import com.kc.gb.feign.ptl.support.enums.PtlLedOnOff;
import com.kc.gb.feign.ptl.support.enums.PtlLightColor;
import com.kc.gb.feign.ptl.support.enums.PtlLightOnOff;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicBoolean;

@Slf4j
@Service
public class ContainerPtlAminServiceImpl implements ContainerPtlAminService {

    @Resource
    StripAdminService stripAdminService;

    @Resource
    PtlHardwareService iDevicePtlService;

    AtomicBoolean DEBUG_MODE = new AtomicBoolean(false);

    @Override
    public boolean changeMode(boolean isDebugMode){
        DEBUG_MODE.set(isDebugMode);
        return isDebugMode;
    }

    @Override
    public boolean getMode(){
        return DEBUG_MODE.get();
    }

    @Override
    public StationContainerSlotLayout getStationContainerSlotArea(Long warehouseId, String stationCode){
        return stripAdminService.getStationContainerSlotLights(warehouseId, stationCode);
    }

    @Override
    public void press(boolean inDebugMode, int lightNo){
        log.info("ContainerPtl|press: debugMode {},lightNo: {}", inDebugMode, lightNo);
        Assert.isTrue(ObjectUtils.isNotEmpty(iDevicePtlService),"PTL deivce not enabled");
        List<PtlLightState>  lightStates = iDevicePtlService.getLightStates(inDebugMode, Arrays.asList(lightNo));
        PtlLightState lightState = lightStates.get(0);
        log.info("ContainerPtl|lightNo:{}  state{}",  lightNo, JSON.toJSONString(lightState));
        if(inDebugMode){
            // 调试模式点亮
            if(PtlLightOnOff.LIGHT_OFF.equals(lightState.getLightStatus())){
                List<AbstractPtlCommand> commands = new ArrayList<>();
                commands.add(new LightCommand(lightNo, PtlLightOnOff.LIGHT_ON, PtlLightColor.RED,0));
                // 生成 1 到 200 之间的随机整数（包含 1 和 200）
                Random random = new Random();
                int randomNum = random.nextInt(200) + 1;
                commands.add(new LedCommand(lightNo, PtlLedOnOff.LED_ON, randomNum));
                iDevicePtlService.consumeCommands(commands,inDebugMode);
            }
            // 调试模式关灯
            else{
                List<AbstractPtlCommand> commands = new ArrayList<>();
                commands.add(new LightCommand(lightNo, PtlLightOnOff.LIGHT_OFF, PtlLightColor.RED,0));
                commands.add(new LedCommand(lightNo, PtlLedOnOff.LED_OFF, 0));
                iDevicePtlService.consumeCommands(commands,inDebugMode);
            }
        }else{
            // 模拟器模式不允许开灯操作
            if(PtlLightOnOff.LIGHT_OFF.equals(lightState.getLightStatus())){
                throw new I18NException(ErrorCode.PTL_CAN_NOT_OPEN_IN_SIMULATOR);
            }
            // 模拟器模式下关灯操作
            else{
                // 模拟拍灯事件
                iDevicePtlService.confirmed(lightNo);
                // 关灯操作
                List<AbstractPtlCommand> commands = new ArrayList<>();
                commands.add(new LightCommand(lightNo, PtlLightOnOff.LIGHT_OFF, PtlLightColor.RED,0));
                commands.add(new LedCommand(lightNo, PtlLedOnOff.LED_OFF, 0));
                iDevicePtlService.consumeCommands(commands,inDebugMode);
            }
        }
    }
}
